package com.wbase.demo

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.work.OneTimeWorkRequest
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.wbase.base.logger.Logger
import com.wbase.demo.work.StatisticWorker
import com.wbase.demo.work.UploadWorker
import com.wbase.permission.helper.ActivityPermissionHelper
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val list = arrayListOf(
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_SMS,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
        ActivityPermissionHelper.checkAndRequestPermissionList(
            this,
            list
        ) { isAllGranted, isAllCantShow, deniedList, list ->
            Logger.i("wangheng", "isAllGranted:$isAllGranted,$isAllGranted")
            list.forEach {
                Logger.i("wangheng", "${it.permission} -> ${it.granted} -> ${it.shouldShow}")
            }
        }

        testOneTimeWork()
        testPeriodicWork()
    }

    private fun testPeriodicWork() {
        val request = PeriodicWorkRequest.Builder(StatisticWorker::class.java,15L,TimeUnit.MINUTES)
            .build()

        WorkManager.getInstance(this).enqueue(request)
    }

    private fun testOneTimeWork() {
        val request = OneTimeWorkRequest.Builder(UploadWorker::class.java)
            .build()

        WorkManager.getInstance(this).enqueue(request)
    }

}